/**
 * 
 */
package com.shine.icis.blo.imp;

import com.shine.cotrun.sql.QueryParameters;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.icis.blo.ICompanyInfoService;
import com.shine.icis.dao.ICompanyInfoDAO;
import com.shine.icis.dto.CompanyInfo;
import com.shine.icis.exception.ICISException;

/**
 * @author zsy
 *
 */
public class CompanyInfoService implements ICompanyInfoService {
	
	private ICompanyInfoDAO companyInfoDAO;

	
	public void setCompanyInfoDAO(ICompanyInfoDAO companyInfoDAO) {
		this.companyInfoDAO = companyInfoDAO;
	}

	@Override
	public void addCompanyInfo(String companyCode, String companyName,
			String settleInvoCode, String ofSaleInvoCode, String settleInvoID,
			String settleInvoAcctNO, String occurDate) throws ICISException {
		if (companyCode == null) {
			throw new IllegalArgumentException();
		}
//		if (!companyInfoDAO.checkId(companyCode)) {
//			throw new ICISException("证券公司代码不能重复");
//		}
		if (companyInfoDAO.findTotalCount() > 0) {
			throw new ICISException("只能新增一条记录");
		}
		CompanyInfo info = new CompanyInfo();
		info.setCompanyCode(companyCode);
		info.setCompanyName(companyName);
		info.setOccurDate(occurDate);
		info.setOfSaleInvoCode(ofSaleInvoCode);
		info.setSettleInvoAcctNO(settleInvoAcctNO);
		info.setSettleInvoCode(settleInvoCode);
		info.setSettleInvoID(settleInvoID);
		info.setRecordNo(companyInfoDAO.findNextRecordNo());
		companyInfoDAO.save(info);
		
	}
	
	@Override
	public void modifyCompanyInfo(String oldCompanyCode, String companyCode, String companyName,
			String settleInvoCode, String ofSaleInvoCode, String settleInvoID,
			String settleInvoAcctNO, String occurDate) throws ICISException {
		if (oldCompanyCode == null || companyCode == null) {
			throw new IllegalArgumentException();
		}
		if (!oldCompanyCode.equals(companyCode) && !companyInfoDAO.checkId(companyCode)) {
			throw new ICISException("证券公司代码不能重复");
		}
		CompanyInfo info = new CompanyInfo();
		info.setCompanyCode(companyCode);
		info.setCompanyName(companyName);
		info.setOccurDate(occurDate);
		info.setOfSaleInvoCode(ofSaleInvoCode);
		info.setSettleInvoAcctNO(settleInvoAcctNO);
		info.setSettleInvoCode(settleInvoCode);
		info.setSettleInvoID(settleInvoID);
		companyInfoDAO.update(oldCompanyCode, info);
		
	}

	@Override
	public void deleteCompanyInfo(String companyCode) {
		if (companyCode == null) {
			return;
		}
		companyInfoDAO.delete(companyCode);
		
	}

	@Override
	public ListPage<CompanyInfo> getCompanyInfoPage(QueryParameters qp) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		return companyInfoDAO.findListPage(qp);
	}
	
	@Override
	public String getCompanyCode() {
		QueryParameters qp = new QueryParameters();
		//设置分页信息
		qp.setPageNo(1);
		qp.setPageSize(1);
		ListPage<CompanyInfo> page = getCompanyInfoPage(qp);
		if (page == null || page.isEmpty()) {
			return null;
		}
		CompanyInfo info = page.getDataList().get(0);
		return info.getCompanyCode();
	}

}
